﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace Bus_proj.website
{
    public partial class data_time_round : System.Web.UI.Page
    {
        DataClasses1DataContext db = new DataClasses1DataContext();
        SqlConnection conn = new SqlConnection("Data Source=MONSTERTUA;Initial Catalog=Bus_pro;User ID=sa1;Password=qwerty123");
        int a,z;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    if (Int32.Parse(Session["login_Access_id"].ToString()) == 3)
                    {
                        BindingGrid();
                        //Panel1.Visible = false;
                        //Panel_tableRound.Visible = false;
                        //Panel2.Visible = false;
                        //Panel_around.Visible = false;
                    }
                    else
                    {
                        Response.Redirect("login.aspx");
                    }
                }
                catch (Exception)
                {
                    Response.Redirect("login.aspx");
                }
                
            }           
        }
        private void BindingGrid()
        {
            int input = Int32.Parse(DropDownList3.SelectedValue);
            var q = from p in db.Table_Routes
                    where p.Route_id == input
                    select p;
            gvEmployeeDetails.DataSource = q.ToList();
            gvEmployeeDetails.DataBind();
        }

        public DateTime ConvestToDate(string s)
        {
            int day = 1;
            int mount = 1;
            int years = 1111;
            try
            {
                string[] stemp = s.Split('-');
                years = Convert.ToInt32(stemp[0]);
                mount = Convert.ToInt32(stemp[1]);
                day = Convert.ToInt32(stemp[2]);
                if (years > 2300)
                {
                    years = years - 543;
                }
                return new DateTime(years, mount, day);
            }
            catch (Exception)
            {

            }
            return new DateTime(years, mount, day);
        }

        public string ConvestToDate_check(string s)
        {
            int day = 1;
            int mount = 1;
            int years = 1111;
            try
            {
                string[] stemp = s.Split('-');
                years = Convert.ToInt32(stemp[0]);
                mount = Convert.ToInt32(stemp[1]);
                day = Convert.ToInt32(stemp[2]);
                if (years > 2300)
                {
                    years = years - 543;
                }
                string years2 = years.ToString();
                string mount2 = mount.ToString();
                string day2 = day.ToString();
                return (years2 + "-" + mount2 + "-" + day2);
            }
            catch (Exception)
            {

            }
            string years3 = years.ToString();
            string mount3 = mount.ToString();
            string day3 = day.ToString();
            return (years3 + "-" + mount3 + "-" + day3);
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            Session.Remove("round_chk");
            //Panel_around.Visible = true;
            Panel_tableRound.Visible = true;
                //Panel1.Visible = false;
            //Panel2.Visible = false;
            //Button5.Visible = true;
            int input = Int32.Parse(DropDownList3.SelectedValue);
            string round_chk = DropDownList3.SelectedValue;
            if (round_chk.Length ==1)
	        {
		        round_chk = "00"+round_chk;
	        }
            else if (round_chk.Length ==2)
	        {
		        round_chk = "0"+round_chk;
	        }           
            Session["round_chk"] = round_chk;
            var q = from p in db.Table_Routes
                    where p.Route_id == input
                    select p;
            gvEmployeeDetails.DataSource = q.ToList();
            gvEmployeeDetails.DataBind();
            //DateTime dateinput = ConvestToDate(TextBox1.Text);
            //string dateinput = ConvestToDate_check(TextBox1.Text);
            //var u = from p in db.Table_Arounds
            //        where p.Route_id == input && p.Around_date == dateinput
            //        select new { p.Around_id, p.Route_id, p.Around_date, p.Table_Time.Time_time, p.Table_Type_Bus.Type_Bus_name };

            //GridView2.DataSource = u.ToList();
            //GridView2.DataBind();
//            string TB_around = @"select Table_Around.Around_id,Table_Around.Route_id,Table_Around.Around_date,Table_Time.Time_time,Table_Type_Bus.Type_Bus_name
//                                from Table_Around
//                                inner join Table_Time
//	                                on Table_Around.Time_id = Table_Time.Time_id
//                                inner join Table_Type_Bus
//	                                on Table_Around.Type_Bus_id = Table_Type_Bus.Type_Bus_id
//                                where Table_Around.Route_id = "+input+"and Table_Around.Around_date = '"+dateinput+"'";
//            SqlCommand sqlcomm = new SqlCommand();
//            string Connection = ConfigurationManager.ConnectionStrings["Bus_proConnectionString"].ConnectionString;
//            SqlConnection sqlconn = new SqlConnection(Connection);
//            SqlDataAdapter da = new SqlDataAdapter(TB_around, sqlconn);
//            DataTable dt = new DataTable();
//            da.Fill(dt);
//            if (dt.Rows.Count <= 0)
//            {
//                Button5.Visible = true;
//            }
//            else
//            {
//                Button5.Visible = false;
//            }
//            GridView2.DataSource = dt;
//            GridView2.DataBind();
        }

        protected void gvEmployeeDetails_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lblEmpID = (Label)e.Row.FindControl("lblEmpID");
                GridView gv_Child = (GridView)e.Row.FindControl("gv_Child");
                string txtempid = lblEmpID.Text;
                DataSet ds = new DataSet();
                conn.Open();
                string cmdstr = "Select * from Table_Time where Route_id=@Route_id";
                SqlCommand cmd = new SqlCommand(cmdstr, conn);
                cmd.Parameters.AddWithValue("@Route_id", txtempid);
                SqlDataAdapter adp = new SqlDataAdapter(cmd);
                adp.Fill(ds);
                cmd.ExecuteNonQuery();
                conn.Close();                
                gv_Child.DataSource = ds;
                gv_Child.DataBind();
            }
        }

        protected void gvEmployeeDetails_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "cmdY")
            {
                try
                {
                    Panel1.Visible = false;
                    a = Convert.ToInt32(e.CommandArgument.ToString());
                    var q = from p in db.Table_Times
                            where p.Time_id == a
                            select p;
                    db.Table_Times.DeleteAllOnSubmit(q);
                    db.SubmitChanges();
                    BindingGrid();
                    ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('ลบเวลาเดินทางเสร็จสิ้น');", true);
                    //Response.Write(@"<script language='javascript'>alert('ลบเวลาเดินทางเสร็จสิ้น');</script>");
                }
                catch (Exception)
                {
                    Panel1.Visible = false;
                    BindingGrid();
                    ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('ไม่สามารถลบเวลาเดินทางได้เนื่องจากมีการใช้งานข้อมูลอยู่');", true);
                    //throw;
                }
            }

            if (e.CommandName == "cmdN")
            {
                //Panel1.Visible = true;
                ModalPopupExtender1.Show();
                
                Session["Route_id"] = Convert.ToInt32(e.CommandArgument.ToString()); 
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            try
            {
                Select SData = new Select();
                z = SData.GetId_time();
                Table_Time x = new Table_Time();
                x.Time_id = z;
                x.Route_id = Int32.Parse(Session["Route_id"].ToString());
                x.Time_time = TextBox2.Text;
                db.Table_Times.InsertOnSubmit(x);
                db.SubmitChanges();
                BindingGrid();
                ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('เพิ่มเวลาเดินทางเสร็จสิ้น');", true);
                //Response.Write(@"<script language='javascript'>alert('เพิ่มเวลาเดินทางแล้ว');</script>");
                Panel1.Visible = false;
                //Panel2.Visible = false;
               // Panel_around.Visible = true;
                Panel_tableRound.Visible = true;
            }
            catch (Exception)
            {
                BindingGrid();
                ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('ไม่สามารถเพิ่มเวลาเดินทางได้');", true);
                //Response.Write(@"<script language='javascript'>alert('ไม่สามารถเพิ่มเวลาเดินทางได้');</script>");
                Panel1.Visible = false;
                //Panel2.Visible = false;
               // Panel_around.Visible = true;
                Panel_tableRound.Visible = true;
                throw;
            }
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            //Panel1.Visible = false;
            ////Panel2.Visible = false;
            ////Panel_around.Visible = true;
            //Panel_tableRound.Visible = true;
        }

        protected void Button5_Click(object sender, EventArgs e)
        {
            Panel1.Visible = false;
            //Panel2.Visible = true;
            //Panel_around.Visible = false;
            Panel_tableRound.Visible = false;
            //Label8.Text = DropDownList3.SelectedItem.ToString();
            //Label10.Text = TextBox1.Text;
        }      

        //protected void Button7_Click(object sender, EventArgs e)
        //{
        //    Panel1.Visible = false;
        //    Panel2.Visible = false;
        //    Panel_around.Visible = true;
        //    Panel_tableRound.Visible = true;
        //}

        //protected void Button6_Click(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        Table_Around x = new Table_Around();
        //        x.Around_id = Int32.Parse(TextBox3.Text);
        //        x.Route_id = Int32.Parse(Label8.Text);
        //        x.Around_date = ConvestToDate(TextBox1.Text);
        //        x.Time_id = Int32.Parse(DropDownList4.SelectedValue);
        //        x.Type_Bus_id = Int32.Parse(DropDownList5.SelectedValue);
        //        x.Bus_Line_id = DropDownList7.SelectedValue;
        //        db.Table_Arounds.InsertOnSubmit(x);
        //        db.SubmitChanges();
        //        Response.Write(@"<script language='javascript'>alert('เพิ่มรอบการเดินทาง');</script>");
        //        Panel2.Visible = false;
        //    }
        //    catch (Exception)
        //    {
        //        Response.Write(@"<script language='javascript'>alert('ไม่สามารถเเพิ่มรอบการเดินทางได้');</script>");
        //        throw;
        //    }
        //}

        //protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
        //{
        //    if (e.CommandName == "cmdY")
        //    {
        //        try
        //        {
        //            a = Convert.ToInt32(e.CommandArgument.ToString());
        //            var q = from p in db.Table_Arounds
        //                    where p.Around_id == a
        //                    select p;
        //            db.Table_Arounds.DeleteAllOnSubmit(q);
        //            db.SubmitChanges();
        //            ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('ไม่สามารถเพิ่มข้อมูลลูกค้าได้');", true);
        //            Response.Write(@"<script language='javascript'>alert('ลบรอบการเดินทางเสร็จสิ้น');</script>");
        //        }
        //        catch (Exception)
        //        {
                    
        //            throw;
        //        }
        //    }
        //}
    }
}